package client

import (
	"context"
	"go-stage/core"
	"go-stage/micro/pb"

	"google.golang.org/grpc"
)

type dictClientImpl interface {
	FindByUUID(client grpc.ClientConnInterface, uuid string) (*pb.DictModel, error)
	FindChildrenByUUID(client grpc.ClientConnInterface, uuid string) (*pb.DictModelArray, error)
	FindGroupByUUID(client grpc.ClientConnInterface, uuid string) (*pb.DictModelArray, error)
	Insert(client grpc.ClientConnInterface, dict *pb.DictModel) (*pb.DictModel, error)
	Update(client grpc.ClientConnInterface, dict *pb.DictModel) (*pb.DictModel, error)
	Remove(client grpc.ClientConnInterface, dict *pb.DictModel) (*pb.DictModel, error)
	FindRootByUUID(client grpc.ClientConnInterface, uuid string) (*pb.DictModel, error)
}

type dictClient struct{}

var DictClient dictClientImpl = (*dictClient)(nil)

// FindByUUID 依据UUID获取字典数据
func (dictClient *dictClient) FindByUUID(client grpc.ClientConnInterface, uuid string) (*pb.DictModel, error) {
	c := pb.NewDictServiceClient(client)
	req := &pb.FindByIdRequest{Uuid: uuid}
	res, err := c.FindByUUID(context.Background(), req)
	if err != nil {
		core.Log.Errorf("dict.FindByUUID error: %v", err)
		return nil, err
	}
	return res, nil
}

// FindChildrenByUUID 依据UUID获取其子级字典数据
func (dictClient *dictClient) FindChildrenByUUID(client grpc.ClientConnInterface, uuid string) (*pb.DictModelArray, error) {
	c := pb.NewDictServiceClient(client)
	req := &pb.FindByIdRequest{Uuid: uuid}
	res, err := c.FindChildrenByUUID(context.Background(), req)
	if err != nil {
		core.Log.Errorf("dict.FindChildrenByUUID error: %v", err)
		return nil, err
	}
	return res, nil
}

// FindGroupByUUID 依据UUID获取当前UUID组所有数据
func (dictClient *dictClient) FindGroupByUUID(client grpc.ClientConnInterface, uuid string) (*pb.DictModelArray, error) {
	c := pb.NewDictServiceClient(client)
	req := &pb.FindByIdRequest{Uuid: uuid}
	res, err := c.FindGroupByUUID(context.Background(), req)
	if err != nil {
		core.Log.Errorf("dict.FindGroupByUUID error: %v", err)
		return nil, err
	}
	return res, nil
}

// Insert 添加字典
func (dictClient *dictClient) Insert(client grpc.ClientConnInterface, dict *pb.DictModel) (*pb.DictModel, error) {
	c := pb.NewDictServiceClient(client)
	res, err := c.Insert(context.Background(), dict)
	if err != nil {
		core.Log.Errorf("dict.FindChildrenByUUID error: %v", err)
		return nil, err
	}
	return res, nil
}

// Update 修改字典
func (dictClient *dictClient) Update(client grpc.ClientConnInterface, dict *pb.DictModel) (*pb.DictModel, error) {
	c := pb.NewDictServiceClient(client)
	res, err := c.Update(context.Background(), dict)
	if err != nil {
		core.Log.Errorf("dict.Update error: %v", err)
		return nil, err
	}
	return res, nil
}

// Remove 删除字典
func (dictClient *dictClient) Remove(client grpc.ClientConnInterface, dict *pb.DictModel) (*pb.DictModel, error) {
	c := pb.NewDictServiceClient(client)
	res, err := c.Remove(context.Background(), dict)
	if err != nil {
		core.Log.Errorf("dict.Update error: %v", err)
		return nil, err
	}
	return res, nil
}

// FindRootByUUID 依据UUID获取当前字典的根节点字典信息
func (dictClient *dictClient) FindRootByUUID(client grpc.ClientConnInterface, uuid string) (*pb.DictModel, error) {
	c := pb.NewDictServiceClient(client)
	req := &pb.FindByIdRequest{Uuid: uuid}
	res, err := c.FindRootByUUID(context.Background(), req)
	if err != nil {
		core.Log.Errorf("dict.FindRootByUUID error: %v", err)
		return nil, err
	}
	return res, nil
}
